<?php
gatekeeper ();

$project = $vars ['entity'];
$user = elgg_get_logged_in_user_entity ();
$members_label = elgg_echo ( 'submana:project:members' ). ", <b>". ($project->min_student - 1). " < số thành viên < " .($project->max_student + 1) . "</b>";
$content .= "<div>";
$content .= elgg_view ( 'input/text', array (
		'name' => 'member1',
		'id' => 'project_member1',
		'value' => $user->username,
		'readonly' => true 
) );

$content .= "</div>";
$member_avalable = 1;
for($i = 1; $i < $project->max_student; $i ++) {
	
	$content .= "<div>";
	
	$content .= elgg_view ( 'input/text', array (
			'name' => 'member' . ($i + 1),
			'id' => 'project_member' . ($i + 1),
			'class' => "required"
	) );
	$content .= "</div>";
	
	$member_avalable ++;
}

// search similar project

// set up search params
$query = $project->title;
$sort = 'relevance';
$order = 'asc';
$search_type = "entities";
$params = array (
		'query' => $query,
		'offset' => 0,
		'limit' => 10,
		'sort' => $sort,
		'order' => $order,
		'search_type' => $search_type,
		'type' => 'object',
		'subtype' => "project" 
// 'tag_type' => $tag_type,
// 'friends' => $friends
);

include 'searchobject.php';
$results_view = "";
$results = searhobject ( $params );

foreach ( $results ['entities'] as $result ) {
	if ($result->guid != $project->guid && $result->approved) {
		$results_view .= "<hr>";
		$results_view .= elgg_view_entity ( $result );
		$results_view .= "<hr>";
	}
}

if ($results_view == "") {
	$similar_project .= "Không có đề tài tương tự";
} else {
	$similar_project .= $results_view;
}

$similar_project_div = "<div id = 'similar-project' style='display:none;'>";
$similar_project_div .= $similar_project;
$similar_project_div .= "</div>";

$url_similar_project = elgg_view ( 'output/url', array (
		'text' => "Đề tài tương tự",
		'href' => "#",
		'onclick' => 'toggle("similar-project"); return false;' 
) );

// ---------------- input/hidden ---------------------
$project_guid = elgg_view ( 'input/hidden', array (
		'name' => 'project_guid',
		'value' => $vars ['project_guid'] 
) );

$group_guid = elgg_view ( 'input/hidden', array (
		'name' => 'group_guid',
		'id' => "group_guid",
		'value' => $vars ['group_guid'] 
) );
$members = elgg_view ( 'input/hidden', array (
		'name' => 'total_member',
		'id' => "total_member",
		'value' => $member_avalable 
) );

$waiting_lable = "Chờ ghép nhóm";
$waiting = elgg_view ( 'input/checkbox', array (
		'name' => "waiting" 
) );

// ------------------- buttons -----------------------
$save_button = elgg_view ( 'input/submit', array (
		'value' => elgg_echo ( 'submana:addprojects:save' ) 
) );
$reset_button = elgg_view ( 'input/reset', array (
		'value' => elgg_echo ( 'submana:addprojects:reset' ) 
) );
echo <<<___HTML
<div>
	<label for="members">$members_label</label>
	<div style="width: 50%; margin: 20px;">
		<div id="project_memeber">
			$content
		</div>
		<div>
			$waiting_lable
			$waiting
		</div>
		<div>
			$next_button
		</div>
	</div>
</div>
<div>
	$members
	$group_guid
	$project_guid
	$save_button
	$reset_button
</div>
<div>
	$url_similar_project
	$similar_project_div
</div>
___HTML;

?>


<script type="text/javascript">

var member = 0;
function addmember(){
	member++;
	var strHTML = '<div>'+
				  '<input type="text" value="" name="member'+member+'" '+
				  'id="project_member'+member+'" class="elgg-input-text">'+
				  '</div>';
	$('#project_memeber').append(strHTML);
	$('#total_member').val(member);
}

function toggle(project_group){
	$("#"+project_group).toggle();
}
</script>

